package com.lyz.mapDemo;

import java.util.*;

/**
 * @ClassName MapDemo2
 * @Description 统计任意学生，春游地点的选择（可选多个）
 * @Author 2214361854
 * @Date 2022/2/22 21:48
 * @Version 1.0
 */
public class MapDemo3 {
    public static void main(String[] args) {
        //全部学生的选择结果（集合嵌套）
        Map<String, List<String>> data = new HashMap<>();
        //学生数量
        int stuends = 10;
        //选择范围：ABCD "西湖","长城","华山","泰山"
        String[] selects = {"A","B","C","D"};
        choice(selects,stuends,data);

//        selects(data,"小亮","A","B","C");
//        selects(data,"小新","B","C");

        System.out.println(data);

        //统计每个景点选择的人数
        HashMap<String, Integer> infos = new HashMap<>();

        //提取所有选择景点的信息
        Collection<List<String>> values = data.values();

        for (List<String> value : values) {
            for (String s : value) {
                if (infos.containsKey(s)) {
                    infos.put(s,infos.get(s)+1);
                } else {
                    infos.put(s,1);
                }
            }
        }
        System.out.println(infos);
    }

    /**
     * 学生选择（每名学生选两次）
     * @param selects
     * @param stuends
     * @param data
     */
    private static void choice(String[] selects,int stuends,Map data) {
        Random r = new Random();
        for (int i = 0; i < stuends; i++) {
            selectsPut(data,i+"",selects[r.nextInt(selects.length)],selects[r.nextInt(selects.length)]);
        }
    }

    /**
     * 录入选择学生结果
     * @param data
     * @param name
     * @param values
     */
    private static void selectsPut(Map data,String name,String ...values) {
        List<String> selects = new ArrayList<>();
        Collections.addAll(selects,values);
        data.put(name,selects);
    }
}
